package wang.aweb.childcar.controller.management;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wang.aweb.childcar.common.ApiRestResponse;
import wang.aweb.childcar.model.pojo.WqPage;
import wang.aweb.childcar.model.query.DepositParam;
import wang.aweb.childcar.model.vo.AgentVO;
import wang.aweb.childcar.service.AgentService;
import wang.aweb.childcar.service.DepositService;

import java.util.Map;

@RestController
@RequestMapping("/management/agentDeposit/")
public class AgentDepositController {
    @Autowired
    private AgentService agentService;
    @Autowired
    private DepositService depositService;
    @ApiOperation("新增押金")
    @PostMapping("add")
    public ApiRestResponse add(@RequestBody Map<String,Object> reqMap){
        Integer agentId=(int)reqMap.get("agent_id");

        Integer money=Math.round(Float.valueOf((String)reqMap.get("money"))*100);
        depositService.add(agentId,money);
        return ApiRestResponse.success();

    }
    @ApiOperation("退费")
    @PostMapping("tuifei")
    public ApiRestResponse tuifei(@RequestBody Map<String,Integer> reqMap){
        depositService.tuifei(reqMap.get("id"));
        return ApiRestResponse.success();
    }
    @ApiOperation("押金列表")
    @PostMapping("getList")
    public ApiRestResponse getList(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, @RequestBody DepositParam depositParam ){
        if(depositParam.getUsername()!=null){
            AgentVO agent=agentService.selectByUsername(depositParam.getUsername());
            if(agent!=null){
                depositParam.setAgentId(agent.getId());
            }else{
                depositParam.setAgentId(0);
            }
        }
        if(depositParam.getMobile()!=null){
            AgentVO agent=agentService.selectByMobile(depositParam.getMobile());
            if(agent!=null){
                depositParam.setAgentId(agent.getId());
            }else{
                depositParam.setAgentId(0);
            }
        }
        PageInfo pageInfo=depositService.getList(page,limit,depositParam);
        WqPage wqPage=new WqPage(pageInfo.getTotal(),pageInfo.getList());

        return ApiRestResponse.success(wqPage);
    }
}
